package com.dubbo.mapper;

import com.dubbo.domain.WalletAmount;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * 钱包收支记录
 * @author lihongwu
 * 2018年12月15日 下午4:03:02
 */
public interface WalletAmountMapper extends Mapper<WalletAmount> {
	
	/**
	 * 安装补助金记录
	 *
	 * @author lihongwu
	 * 2018年12月15日 下午4:05:34
	 * @param userId
	 * @param pageStart
	 * @param pageTotal
	 * @return
	 */
	@Select("SELECT" + 
			"	wa.id," + 
			"	wa.amount," + 
			"	wa.note," + 
			"	o.orderNum," + 
			"	o.placeTime," + 
			"	o.totalPrice," + 
			"	op.sendTime," + 
			"  IF(op.isGrants = 1,'已安排','未安排') AS 'status'" + 
			" FROM" + 
			"	w_amount wa" + 
			" LEFT JOIN w_wallet_key wwk ON wa.id = wwk.amountId" + 
			" LEFT JOIN w_wallet w ON wwk.walletId = w.id" + 
			" LEFT JOIN u_wallet_key uwk ON w.id = uwk.walletId" + 
			" LEFT JOIN u_user u ON uwk.userId = u.id" + 
			" LEFT JOIN w_amount_order_key waok ON wa.id = waok.amountId" + 
			" LEFT JOIN o_order o ON waok.orderId = o.id" + 
			" LEFT JOIN o_process op ON o.id = op.orderId" + 
			" WHERE wa.note = '安装补助金'" + 
			" AND u.id = #{userId}" + 
			" ORDER BY wa.create_time DESC" + 
			" LIMIT #{pageStart},#{pageTotal}")
	List<Map<String,Object>> installSubsidyDetail(
            @Param("userId") String userId
            , @Param("pageStart") Long pageStart
            , @Param("pageTotal") Long pageTotal);
	
	/**
	 * 安装补助金记录总数
	 *
	 * @author lihongwu
	 * 2018年12月15日 下午4:05:34
	 * @param userId
	 * @return
	 */
	@Select("SELECT" + 
			"	COUNT(wa.id)" +
			" FROM" + 
			"	w_amount wa" + 
			" LEFT JOIN w_wallet_key wwk ON wa.id = wwk.amountId" + 
			" LEFT JOIN w_wallet w ON wwk.walletId = w.id" + 
			" LEFT JOIN u_wallet_key uwk ON w.id = uwk.walletId" + 
			" LEFT JOIN u_user u ON uwk.userId = u.id" + 
			" LEFT JOIN w_amount_order_key waok ON wa.id = waok.amountId" + 
			" LEFT JOIN o_order o ON waok.orderId = o.id" + 
			" LEFT JOIN o_process op ON o.id = op.orderId" + 
			" WHERE wa.note = '安装补助金'" + 
			" AND u.id = #{userId}")
	Long installSubsidyDetailCount(@Param("userId") String userId);
	
	/**
	 * 提现记录
	 *
	 * @author lihongwu
	 * 2018年12月15日 下午5:05:02
	 * @param userId
	 * @param pageStart
	 * @param pageTotal
	 * @return
	 */
	@Select("SELECT" + 
			"	wa.id," + 
			"	wa.create_time," + 
			"	wa.update_time," + 
			"	CASE wa.`status`" + 
			"	WHEN 0 THEN '审核中'" + 
			"	WHEN 1 THEN '提现成功'" + 
			"	WHEN 2 THEN '提现失败'" + 
			"	END AS 'status'," + 
			"	wa.amount," + 
			"	CASE wa.payType" + 
			"	WHEN 1 THEN '微信'" + 
			"	WHEN 2 THEN '支付宝'" + 
			"	END AS 'drawithType'," + 
			"	wa.accountName," + 
			"	wa.note" + 
			" FROM" + 
			"	w_amount wa" + 
			" LEFT JOIN w_wallet_key wwk ON wa.id = wwk.amountId" + 
			" LEFT JOIN w_wallet w ON wwk.walletId = w.id" + 
			" LEFT JOIN u_wallet_key uwk ON w.id = uwk.walletId" + 
			" LEFT JOIN u_user u ON uwk.userId = u.id" + 
			" WHERE u.id = #{userId}" + 
			" AND wa.`status` IN (0,1,2)" + 
			" ORDER BY wa.create_time DESC" + 
			" LIMIT #{pageStart},#{pageTotal}")
	List<Map<String,Object>> drawithDetail(
            @Param("userId") String userId
            , @Param("pageStart") Long pageStart
            , @Param("pageTotal") Long pageTotal);
	
	/**
	 * 提现记录总数
	 *
	 * @author lihongwu
	 * 2018年12月15日 下午5:07:43
	 * @param userId
	 * @return
	 */
	@Select("SELECT" + 
			"	COUNT(wa.id)" + 
			" FROM" + 
			"	w_amount wa" + 
			" LEFT JOIN w_wallet_key wwk ON wa.id = wwk.amountId" + 
			" LEFT JOIN w_wallet w ON wwk.walletId = w.id" + 
			" LEFT JOIN u_wallet_key uwk ON w.id = uwk.walletId" + 
			" LEFT JOIN u_user u ON uwk.userId = u.id" + 
			" WHERE u.id = #{userId}" + 
			" AND wa.`status` IN (0,1,2)")
	Long drawithDetailCount(@Param("userId") String userId);
	
}